package com.xiaoyu;

import com.xiaoyu.model.Class;
import com.xiaoyu.model.Course;
import com.xiaoyu.model.Student;
import com.xiaoyu.model.Teacher;
import com.xiaoyu.service.ClassService;
import com.xiaoyu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "课程信息接口")
//@CrossOrigin(origins = "http://39.101.65.116:8080")
@CrossOrigin(origins = "http://localhost:8080")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private ClassService classService;

    @ApiOperation("学生课程信息查询接口")
    @GetMapping("/courseInfo")
    public List<Teacher> courseInfo(HttpSession session){
        Student s=(Student) session.getAttribute("student");
        if (s!=null){
            Class c=new Class();
            c.setId(s.getCid());
            return classService.findOneClass(c).getTeacherList();
        }
        return null;
    }

    @ApiOperation("教师课程信息查询接口")
    @GetMapping("/courseInfoT")
    public List<Class> courseInfoT(HttpSession session){
        Teacher t=(Teacher) session.getAttribute("teacher");
        if (t!=null){
            List<Class> classList= classService.findSomeClass(t);
            return classList;
        }
        return null;
    }

    @ApiOperation("课程列表接口")
    @GetMapping("/courseList")
    public List<Course> courseList(){
        return courseService.allCourse();
    }

    @ApiOperation("课程添加接口")
    @PutMapping("/course")
    public String addCourse(@RequestBody Course course){
        if(!course.getCourse().equals("")){
            courseService.addCourse(course);
            return "success";
        }
            return "error";
    }

    @ApiOperation("课程删除接口")
    @DeleteMapping("/course")
    public String deleteCourse(@RequestBody Course course){
        if(course.getId()!=null){
            courseService.deleteCourse(course);
            return "success";
        }
            return "error";
    }

    @ApiOperation("课程修改接口")
    @PostMapping("/course")
    public String updateCourse(@RequestBody Course course){
        if(course.getId()!=null&&!course.getCourse().equals("")){
            courseService.updateCourse(course);
            return "success";
        }
            return "error";
    }
}
